//--map- -
void function($){
    if($.define){
        return ;
    }
    // 常量简化
    var doc = document
        ,   docEl = doc.documentElement
        ,   head = doc.head || doc.getElementsByTagName("head")[0] || docEl
        ,   slice = Array.prototype.slice
        ,   pop = Array.prototype.pop
        ,   toString = Object.prototype.toString
        ,   isTouch  = 'ontouchstart' in document;

    //数据循环
    var forEach = function(){
        function forpush(arr,v){
            arr.push(v);
            return v;
        }
        function forappend(obj,v,k){
            obj[k] = v;
        }
        function forback(){
            return arguments[1];
        }
        return function (o, fn, exe, scope){
            if(scope == null){
                scope = this;
            }
            if(o){
                var doExe = exe? exe.push ? forpush : forappend : forback;
                var len = o.length;
                var type = toString.call(o).toLowerCase();
                var v;
                if(type == "[object array]" || type == "[object nodelist]" || type == "[object htmlcollection]" || type == "[object arguments]" || (type == "[object object]" && typeof len == "number" && len >= 0)){
                    for(var i = 0; i < len; i += 1){
                        v = fn.call(scope, o[i], i);
                        if(v === false){
                            break;
                        }
                        doExe(exe,v,i);
                    }
                }
                else{
                    for(var n in o){
                        if(o.hasOwnProperty(n)){
                            v = fn.call(scope, o[n], n);
                            if(v === false){
                                break;
                            }
                            doExe(exe,v, n);
                        }
                    }
                }
            }
            return exe || scope;
        }
    }();

    //扩展object
    function extra(k,v){
        if(typeof k == "string"){
            this[k] = v;
            return this;
        }
        var ag = slice.call(arguments), m,n;
        while(ag.length){
            m = ag.shift();
            for(n in m){
                this[n] = m[n];
            }
        }
        return this;
    }

    // 继承
    function extend(){
        var ag = slice.call(arguments),m;
        if(typeof this == "function"){
            this.prototype.extra = extra;
            this._inits_ = [];
            while(ag.length){
                m = ag.shift();
                if(typeof m == "function"){
                    extra.call(this,m);
                    this._inits_.unshift(m);
                    m = m.prototype;
                }
                extra.call(this.prototype,m);
            }
        }
        else{
            while(m = ag.shift()){
                if(typeof m == "function"){
                    try{
                        m = new m();
                    }catch(e){
                        m = m.prototype;
                    }
                }
                extra.call(this,m);
            }
        }
        return this;
    }

    // 模拟事件类
    var EventEmitter = extend.call(function (){},{
        on:function(type, fn){
            var m = this._monitor_ || (this._monitor_ = {});
            m[type] || (m[type] = []);
            m[type].push(fn);
            return this;
        },
        hasEvent:function(key){
            if(this['on' + key]){
                return true;
            }
            var m = this._monitor_ && this._monitor_[key];
            return m && m.length > 0;
        },
        onec:function(type, fn){
            this.on(type,function(){
                fn.apply(this,arguments);
                this.off(type,arguments.callee);
            });
            return this;
        },
        off:function(type, fn){
            var m = this._monitor_;
            if(m){
                if(fn){
                    var es = m[type];
                    if(es){
                        for(var i = 0; i < es.length; i += 1){
                            if(es[i] == fn){
                                es.splice(i, 1);
                                break;
                            }
                        }
                    }
                }
                else if(type){
                    delete m[type];
                }
                else{
                    delete this._monitor_;
                }
            }
            return this;
        },
        emit:function(){
            var type = Array.prototype.shift.call(arguments),
                es = this._monitor_?this._monitor_[type]:null,
                flag, i;
            //返回false 阻止冒泡
            if(es){
                var m;
                for(i = 0; i < es.length; i += 1){
                    m = es[i].apply(this, arguments);
                    flag!==false && (flag = m);
                }
            }

            //以下是冒泡事件
            if(flag!==false){
                es = this.constructor._monitor_?this.constructor._monitor_[type]:null;
                if(es){
                    for(i = 0; i < es.length; i += 1){
                        es[i].apply(this, arguments);
                    }
                }
            }
            var t = "on" + type;
            if(typeof this[t] == "function"){
                return this[t].apply(this, arguments);
            }
            return this;
        }
    });
    //函数的事件，触发，有具体对象触发
    EventEmitter.on = EventEmitter.prototype.on;
    EventEmitter.onec = EventEmitter.prototype.onec;
    EventEmitter.off = EventEmitter.prototype.off;

    //当前脚本的script节点
    var baseKey = 'data-main';
    var jsNode = function(){
        var nodes = doc.getElementsByTagName("script");
        var node;
        for(var i = nodes.length - 1 ;i > -1 ;i -= 1){
            if(nodes[i].getAttribute(baseKey) != null){
                node = nodes[i];
                break;
            }
            if(!node && nodes[i].src){
                node = nodes[i];
            }
        }
        return node;
    }();
    // 不带参数的URL
    var jsNodeFullUrl = jsNode.hasAttribute?jsNode.src:jsNode.getAttribute('src',4);
    //console.log(jsNodeFullUrl);
    var jsNodeUrl = jsNodeFullUrl.split(/[\?\#]/)[0];
    //console.log((jsNode.hasAttribute?jsNode.src:jsNode.getAttribute('src',4)));
    // 参数
    var jsNodeMain = jsNode.getAttribute(baseKey) || '';

    // 配置
    var $C = {
        baseURL:jsNodeMain.split(/[\?\#]/)[0].replace(/[^\/]*$/, ""),
        suffix:jsNodeUrl.split(/\/+/).pop().replace(/^[^.]+/,''),
        version:jsNodeFullUrl.replace(jsNodeUrl,'').replace(/[?#&=]/,'').slice(-10),
        paths:{
            // sky的当前路径
            'sky':jsNodeUrl.replace(/\/[^\/]*$/, "")
        },
        alias:{}
    }
    //console.log('$C', $C);
    $.config = function(option){
        if(!option){
            return ;
        }
        if(option.paths){
            extra.call($C.paths,option.paths);
        }
        if(option.alias){
            extra.call($C.alias,option.alias);
        }
        if(option.baseURL){
            $C.baseURL = option.baseURL;
        }
        if(option.version){
            $C.version = option.version;
        }
        if(option.suffix){
            $C.suffix = option.suffix;
        }
    };

    // 别名转换为真实的 url
    var xAlias = {};
    function fixUrl(url,flag){
        if(/^http(?:s)?:\/\//i.test(url)){
            return url;
        }
        if(/^\/{2}/i.test(url)){
            return window.location.protocol + url;
        }
        if(/^\//i.test(url)){
            return window.location.protocol + '//' + window.location.host + (window.location.port == 80?'':(':' + window.location.port)) + url;
        }
        //console.log(url, flag);
        if(flag == 1){
            return fixUrl($C.baseURL + url.replace(/^\.\//,''),2);
        }
        if(flag == 2){
            var path = window.location.pathname.replace(/[^\/]*$/,url.replace(/^\.\//,''));
            // 带 ../ 转换为真实地址
            while(/\.\.\//.test(path)){
                path = path.replace(/(?:[^\/]+\/+)?\.\.\//,'');
            }
            return window.location.protocol + '//' + window.location.host + path;
        }
        var ps = url.match(/^([^/:]+)(\/.+)$/);
        if(ps && $C.paths[ps[1]]){
            url = $C.paths[ps[1]] + ps[2];
        }
        return fixUrl(url,1);
    }
    function getUrl(src){
        //console.log('getUrl0',arguments.callee.caller);
        var x = xAlias[src] || $C.alias[src] || src;
        //console.log('getUrl1',src,x);
        var u = x.match(/^([^?#]*)(?:\?*([^#]*))?(#.*)?$/);
        var u1 = u[1] || '';
        var u2 = u[2] || '';
        if(u2.indexOf($C.version) < 0){
            u2 = u2?$C.version + '&' + u2:$C.version;
        }
        if(u2){
            u2 = '?' + u2;
        }
        return fixUrl((/\.js|\.css$/.test(u1)?u1:(u1 + $C.suffix)) + u2 + (u[3] || ''),/\.js$/.test(u1)?2:0).match(/^([^#]*)(#.*)?$/);
    }

    // 事件
    var events = $.events = new EventEmitter();

    //js加载
    function loadJS(url, callback, charset){
        var node = doc.createElement("script");
        node.setAttribute("type", "text/javascript");
        if(charset){
            node.setAttribute("charset", charset);
        }
        function onload(){
            node.onreadystatechange = node.onload = node.onerror = null;
            callback && callback(url);
            //防止回调的时候，script还没执行完毕
            setTimeout(function(){
                node.parentNode.removeChild(node);
                node = null;
            },20);
        }
        if("onload" in node){
            node.onload = onload;
            node.onerror = function(){
                events.emit('error',{url:url,node:node});
                onload();
            }
        }
        else{
            node.onreadystatechange = function(){
                if(/loaded|complete/.test(node.readyState)){
                    onload();
                }
            }
        }

        node.async = true;
        head.appendChild(node);
        node.src = url;
        return node;
    }

    // 加载css
    function loadCSS(url, charset){
        var link = doc.createElement('link');
        link.setAttribute('rel','stylesheet');
        link.setAttribute('type','text/css');
        if(charset){
            link.setAttribute('charset',charset);
        }
        head.appendChild(link);
        link.href = url;
    }

    // 当前 script的url
    function getCurrentScriptSrc(){
        //取得正在解析的script节点
        if(doc.currentScript) { //firefox 4+
            return doc.currentScript.src;
        }

        // 参考 https://github.com/samyk/jiagra/blob/master/jiagra.js
        var stack;
        try {
            //强制报错,以便捕获e.stack
            eval("a.b.c()");
        } catch(e) {//safari的错误对象只有line,sourceId,sourceURL
            //console.log('stack',e.stack);
            stack = e.stack;
            if(!stack && window.opera){
                //opera 9没有e.stack,但有e.Backtrace,但不能直接取得,需要对e对象转字符串进行抽取
                stack = (String(e).match(/of linked script \S+/g) || []).join(" ");
            }
        }
        if(stack) {
            /**e.stack最后一行在所有支持的浏览器大致如下:
             *chrome23:
             * at http://113.93.50.63/data.js:4:1
             *firefox17:
             *@http://113.93.50.63/query.js:4
             *opera12:
             *@http://113.93.50.63/data.js:4
             *IE10:
             *  at Global code (http://113.93.50.63/data.js:4:1)
             */
            stack = stack.split( /[@ ]/g).pop();//取得最后一行,最后一个空格或@之后的部分
            stack = stack[0] == "(" ? stack.slice(1,-1) : stack;
            return stack.replace(/(:\d+)?:\d+$/i, "");//去掉行号与或许存在的出错字符起始位置
        }

        var nodes = head.getElementsByTagName("script"); //只在head标签中寻找
        for(var i = 0, node; node = nodes[i++];) {
            //console.log("node", node, node.src, node.readyState);
            if(node.readyState === "interactive") {
                return node.hasAttribute?node.src:node.getAttribute('src',4);
            }
        }

        return '';
    }

    // 顺序或者非顺序加载
    var request = function(){
        //已经加载完毕的js
        var jsLoaded = {};
        jsLoaded[jsNodeUrl] = true;
        jsLoaded[jsNodeUrl + '?' + $C.version] = true;

        return function(disorder){
            //待加载的js
            var jsWait = [];
            //待回调的函数
            var callWait = [];
            //js是否在执行中
            var jsFlag = 0;

            function requireJSed(url){
                //alert(url)
                var x = jsLoaded[url];
                jsLoaded[url] = true;
                if(x && x!==true){
                    for(var i= 0,n;i<x.length;i+=1){
                        n = x[i];
                        n[0](n[1]);
                    }
                }
            }

            // 支持加载css的
            function requireJS(src, callBack, charset){
                var url = getUrl(src)[1];
                if(jsLoaded[url] === true){
                    callBack(src);
                    return;
                }
                if(jsLoaded[url] == null){
                    jsLoaded[url] = [[callBack,src]];
                    if(/\.css$/i.test(url.split('?')[0])){
                        loadCSS(url,charset);
                        setTimeout(function(){
                            requireJSed(url);
                        },0);
                    }
                    else{
                        loadJS(url,requireJSed,charset);
                    }
                }
                else{
                    jsLoaded[url].push([callBack,src]);
                }
            }

            //无序下载
            function disorderJS(urls, callBack, charset){
                //console.log('disorderJS+one', urls)
                var isStr = typeof urls == "string";
                if(isStr || urls.length == 1){
                    requireJS(isStr?urls:urls[0], callBack, charset);
                    return;
                }
                var led = {};
                for(var i=0;i<urls.length;i+=1){
                    led[urls[i]] = true;
                }
                function loadBack(src){
                    delete led[src];
                    for(var n in led){
                        return;
                    }
                    //debugger;
                    //console.log('loadBack',urls,callBack)
                    callBack();
                    loadBack = function(){};
                }
                for(i=0;i<urls.length;i+=1){
                    requireJS( urls[i], loadBack, charset);
                }
                return;
            }

            //出栈
            function stackShift(){
                if(jsWait.length){
                    var js = jsWait.shift();
                    //console.log(js);
                    stackPush.currSrc = js[0];
                    disorderJS.apply(null, js);
                    return ;
                }
                if(callWait.length){
                    var back = callWait.pop();
                    back();
                    setTimeout(stackShift,0);
                    return ;
                }
                jsFlag = 0;
            }

            //进栈
            function stackPush(urls, callBack, charset){
                if(callBack){
                    callWait.push(callBack);
                }
                for(var i = 0; i < urls.length; i += 1){
                    if(disorder || typeof urls[i] == 'string'){
                        jsWait.push([urls[i], stackShift, charset]);
                    }
                    else{
                        stackPush(urls[i], null, charset);
                    }
                }
                if(jsFlag == 0){
                    jsFlag = 1;
                    stackShift();
                }
            }
            stackPush.backCall = function(back){
                callWait.unshift(back);
            }
            stackPush.currSrc = jsNodeUrl;
            return stackPush;
        }
    }();

    // define 加载用
    var canDisorder = getCurrentScriptSrc() !== '';
    //console.log('canDisorder' , canDisorder);
    var definePush = request(canDisorder);

    //模块化
    var xModules = {};
    function setModule(url,index,deps){
        url = url.replace(/(?:#[^#]*)?$/,'');
        var m = {};
        //console.log('xxxx', url);
        m.dir = url.split(/[?#]/)[0].replace(/[^\/]*$/,'');
        m.index = index;
        m.deps = deps || [];
        m.exports = {};
        if(index){
            //if(!xModules[url]){
            //    // 无默认,第一个就是默认的
            //    xModules[url] = m;
            //}
            url += '#' + index;
            xAlias[index] = url;
        }
        m.url = url;
        xModules[url] = m;
        return m;
    }
    function getModule(id){
        return xModules[id] || xModules[getUrl(id)[0]] || {};
    }
    function getExports(id){
        var m = getModule(id);
        return m.exports;
    }

    function requireEnd(urls,arr){
        forEach(urls,function(v){
            if(v.push && v.shift){
                requireEnd(v,arr);
            }
            else if(!/\.css$/.test(v.split(/[#?]/)[0])){
                //console.log(v, getExports(v))
                arr.push(getExports(v));
            }
        });
        return arr;
    }

    function isAllEnd(deps){
        //console.log('isAllEnd1:::' + deps)
        var flag = true;
        forEach(deps,function(dep){
            //console.log('isAllEnd222:::',dep)
            if(typeof dep == 'string'){
                if(!/\.css$/.test(dep.split(/[#?]/)[0])){
                    var m = getModule(dep);
                    //console.log('isAllEnd333---:::',dep,m,m.exports,m.isEnd,xModules)
                    if(!m.isEnd){
                        //console.log('isAllEnd444:::',m,dep,deps)
                        return flag = false;
                    }
                }
            }
            else if(!isAllEnd(dep)){
                return flag = false;
            }
        });
        return flag;
    }

    function require(){
        if(arguments.length == 1 && typeof arguments[0] == 'string' && !/\.css(?:[?#]+.*)?$/.test(arguments[0])){
            return getExports(arguments[0]);
        }
        var urls = slice.call(arguments),len = urls.length,callBack,charset,back;
        //console.log('require1',urls)
        if(typeof urls[len - 2] == "function"){
            charset = urls.pop();
            len -= 1;
        }
        if(typeof urls[len - 1] == "function"){
            callBack = urls.pop();
        }
        if(callBack){
            back = function(){
                //console.log('require2',urls.join(),isAllEnd(urls))
                if(isAllEnd(urls)){
                    callBack.apply($,requireEnd(urls,[]));
                }
                else{
                    definePush.backCall(back);
                }
            }
        }
        //console.log('require',urls);
        definePush(urls,back,charset);
    }

    function defineEnd(factory,m){
        if(typeof factory == "function"){
            var arr = requireEnd(m.deps,[function(id){
                return getExports(m.deps && m.deps[id] || id);
            }, m.exports, m]);
            var y = factory.apply(m, arr);
            if(y != null){
                m.exports = y;
            }
        }
        else{
            m.exports = factory;
        }

        var key = '#' + (m.index || '');
        if(key != '#' || !events.hasEvent(key)){
            key = m.url;
        }
        events.emit(key,m.exports,m);
        //console.log("emit", key,m);
        m.isEnd = true;
    }

    $.onDefine = function(key, fn){
        if(!/^#+/.test(key)){
            key = getUrl(key)[0];
        }
        //console.log('define',key);
        events.on(key, fn);
        return $;
    };

    function define(){
        var ag = slice.call(arguments);
        var factory = ag.pop(),deps = [],index = "";
//      if(typeof factory == 'string'){
//          factory = ag.pop();
//      }
        if(ag.length){
            deps = ag.pop();
            if(typeof deps == "string"){
                index = deps;
                deps = [];
            }
            else{
                index = ag.pop() || '';
            }
        }
        //console.log(index);
        //console.log("define1",deps,'define1::1::' + getCurrentScriptSrc())
        var m = setModule(getCurrentScriptSrc() || getUrl(definePush.currSrc)[1],index,deps);
        if(deps && deps.length){
            //console.log('deps+++1', deps)
            function back(){
                //console.log('deps+++2', deps, isAllEnd(deps))
                if(isAllEnd(deps)){
                    defineEnd(factory,m);
                }
                else{
                    definePush.backCall(back);
                }
            }
            //console.log('deps+++', deps)
            definePush([deps],back);
        }
        else{
            //console.log('deps+++3', m)
            defineEnd(factory,m);
            //console.log('deps+++4', m)
        }
    }
    // 部分使用
    define.amd = true;
    define.on = $.onDefine;

    // 基础 函数
    define('util',function(require, exports){
        var uAgent = window.navigator.userAgent.toLowerCase();
        //IE 版本 非IE为 0
        exports.IE = (window.attachEvent && window.ActiveXObject && !window.opera) ? parseFloat(uAgent.match(/msie ([\d.]+)/)[1]) : 0;
        //Firefox 版本
        exports.Firefox = /firefox\/([\d.]+)/.test(uAgent) ? parseFloat(RegExp.$1) : 0;
        exports.isTouch = isTouch;

        //绑定事件方法以及参数
        function bind(){
            var a = slice.call(arguments), m = a.shift(), o = a.shift();
            return function(){
                return m.apply(o == null ? this : o, a.concat(slice.call(arguments)));
            }
        }

        exports.bind = bind;
        exports.extra = function(){
            return extra.apply(pop.call(arguments),arguments);
        }
        //继承与扩展
        exports.extend = function(){
            return extend.apply(pop.call(arguments),arguments);
        }

        exports.loadJS = loadJS;
        exports.loadCSS = loadCSS;
        exports.forEach = forEach;

        // 获取页面唯一的 id 值
        var soleTime = new Date().getTime() - 10000;
        var soleCount = 100;
        exports.getSole = function(){
            soleCount += 1;
            return Number(Math.round(Math.random() * 100 + soleCount) + '' + (new Date().getTime() - soleTime)).toString(36);
        }

        exports.trim = function(str){
            if(str){
                return str.replace(/^\s+/,"").replace(/\s+$/,"");
            }
            return '';
        }

        //去除HTML编码 生成可以在html文档显示的字符
        exports.htmlEncode = function(str){
            return (str || '').replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&#34;").replace(/\'/g,"&#39;");
        }
        exports.htmlEncodeBr = function(str){
            return exports.htmlEncode(str).replace(/\n/g,"<br />");
        }
    });

    // EventEmitter
    define('emitter', function(){
        return EventEmitter;
    });
    $.get = getExports;
    $.require = require;
    $.define = define;
    if(!window.require){
        window.require = require;
    }
    if(!window.define){
        window.define = define;
    }

    // 加载非 模块化的js
    var loadPush = request();
    $.include = function(){
        var urls = slice.call(arguments),len = urls.length,callBack,charset;
        if(typeof urls[len - 2] == "function"){
            charset = urls.pop();
            len -= 1;
        }
        if(typeof urls[len - 1] == "function"){
            callBack = urls.pop();
        }
        loadPush(urls,callBack,charset);
    }
    if(!window.include){
        window.include = $.include;
    }

    $.ready = function(fn){
        // domready
        doc.attachEvent ? doc.attachEvent("onreadystatechange", function(){
            if(doc.readyState == "complete" || doc.readyState == "loaded"){
                fn();
            }
        }) : doc.addEventListener("DOMContentLoaded", fn, false);
    };

    // 启动mian
    if(jsNodeMain){
        $.ready(function(){
            $.include(jsNodeMain.split(/\/+/).pop());
        });
    }
}(window.sky || (window.sky = {}));